package framework;

import java.util.Iterator;
import java.util.Vector;

/**
 * This class models an order. It contains the code of an order, the total cost of an order,
 * and a collection of <code>OrderItem</code> assigned to an order.
 *
 * @see OrderItem
 */
public class Order {
	
	/**
	 * The order items assigned to an order.
	 */
	private Vector<OrderItem> orderItems = new Vector<OrderItem>();
	
	/**
	 * The code of the order.
	 */
	private int orderCode = 0;
	/**
	 * The total cost of the order.
	 */
	private double totalCost = 0;

	/**
	 * Get the code of the order.
	 * @return the code of the order
	 */
	public int getOrderCode() {
		return orderCode;
	}
	
	/**
	 * Set a new code for the order.
	 * @param newCode a new code for the order
	 */
	public void setOrderCode(int newCode) {
		orderCode = newCode;
	}
	/**
	 * Get the total cost of the order.
	 * @return the total cost of the order
	 */
	public double getTotalCost() {
		
		for(OrderItem item : orderItems) {
			totalCost += item.getQuantity()*item.getProduct().getPrice();
		}
		
		return totalCost;
	}
	
	/**
	 * Get the specific order item in the order according to the number.
	 * @param index the number of the order item
	 * @return the specific order item
	 */
	public OrderItem getItem(int index) {
		return orderItems.get(index);
	}
	
	/**
	 * Get the specific order item in the order according to the product code.
	 * @param code the product code of the order item
	 * @return the specific order item
	 */
	public OrderItem getItem(String code) {
		for(OrderItem item : orderItems) {
			if (item.getProduct().getCode().equals(code)) {
				return item;
			}
		}
		return null;
		
	}
	
	/**
	 * Add an order item into the order.
	 * @param newOrderItem the order item to be added in
	 */
	public void addItem(OrderItem newOrderItem) {
		orderItems.add(newOrderItem);
	}
	
	/**
	 * Remove an order item from the order.
	 * @param item the order item to be removed
	 */
	public void removeItem(OrderItem item) {
		orderItems.remove(item);
	}
	
	/**
	 * Show the information of an order.
	 */
	public String toString() {
		String str = "Order Code: " + orderCode;
		for(OrderItem item : orderItems) {
			str += item.toString();
			str += "\n";
		}
		str = str + "Order Total: " + getTotalCost();
		return str;
	}

	public Iterator<OrderItem> getOrderItemsIterator(){
		return orderItems.iterator();
	}
}
